package com.qgs.waf.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class RandomUtil {
	public static void main(String[] args) {
		for (int i = 0; i <100; i++) {
			System.out.println(randomDate(null, null));
		}
	}

	public static Date randomDate(String beginDate, String endDate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		beginDate = (StringUtils.isBlank(beginDate)) ? "2000-01-01" : beginDate;
		endDate = (StringUtils.isBlank(endDate)) ? format.format(new Date()) : beginDate;
		try {
			Date start = format.parse(beginDate);// 开始日期
			Date end = format.parse(endDate);// 结束日期
			if (start.getTime() >= end.getTime()) {
				return null;
			}
			long date = random(start.getTime(), end.getTime());

			return new Date(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static long random(long begin, long end) {
		long rtnn = begin + (long) (Math.random() * (end - begin));
		if (rtnn == begin || rtnn == end) {
			return random(begin, end);
		}
		return rtnn;
	}
}
